﻿using Data.Context;
using Data.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Data.Logic
{
    public class QuestionAndAnswerLogic
    {

        public QuestionAndAnswerModel ToModel(QuestionAndAnswerEntity entity)
        {
            return entity != null ? new QuestionAndAnswerModel {
                answer=entity.answer,answer_length=entity.answer_length,answer_nosign=entity.answer_nosign,datecreate=entity.datecreate,datemodified=entity.datemodified,datepublish=entity.datepublish,email_answer=entity.email_answer,emailcreate=entity.emailcreate,id=entity.id,image=entity.image,image_thumb=entity.image_thumb,is_help=entity.is_help,
                likes=entity.likes.Value,main_qa=entity.main_qa,views=entity.views.Value,zuicoins=entity.zuicoins,is_complete=entity.is_complete
            } : null;
        }

        public bool Insert(QuestionAndAnswerEntity entity)
        {
            try
            {
                using (var db = DataFactory.CreateContext())
                {
                    var objQA = db.QuestionAndAnswer.Where(a => a.answer.ToLower() == entity.answer.ToLower() && a.answer_nosign == entity.answer_nosign).FirstOrDefault();
                    if (objQA == null)
                    {
                        db.QuestionAndAnswer.AddObject(entity);
                        db.SaveChanges();
                        return true;
                    }
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
        public bool Update(QuestionAndAnswerEntity entity)
        {
            try
            {
                using (var db = DataFactory.CreateContext())
                {
                    var objQA = db.QuestionAndAnswer.Where(a=>a.id==entity.id).FirstOrDefault();
                    if (objQA != null)
                    {
                        db.QuestionAndAnswer.Attach(entity);
                        
                        db.SaveChanges();
                        return true;
                    }
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        public QuestionAndAnswerEntity getQAInfo(int qaId)
        {
            try
            {
                using (var db = DataFactory.CreateContext())
                {
                    var objQA = db.QuestionAndAnswer.Where(a => a.id == qaId).FirstOrDefault();
                    return objQA;
                }
            }
            catch (Exception)
            {

                return null;
            }
        }

        public List<QuestionAndAnswerEntity> getListQAByEmail(string email)
        {
            try
            {
                using (var db = DataFactory.CreateContext())
                {
                    var lstQA = db.QuestionAndAnswer.Where(a => a.emailcreate == email).OrderByDescending(a => a.datecreate).ToList();
                    return lstQA;
                }
            }
            catch (Exception)
            {

                return null;
            }
        }

        public List<QuestionAndAnswerEntity> getListQAPaging(string email, int page, int size, out int total)
        {
            try
            {
                using (var db = DataFactory.CreateContext())
                {
                    var lstQA = db.QuestionAndAnswer.Where(a => a.emailcreate == email).OrderByDescending(a => a.datecreate).ToList();
                    total = lstQA.Count;

                    return lstQA.Skip(page-1).Take(size).ToList();
                }
            }
            catch (Exception)
            {
                total = 0;
                return null;
            }
        }
    }
}
